package com.hoo.service.equipment;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.mapper.BeanMapper;
import com.hoo.entity.equipment.InvoiceMachine;
import com.hoo.entity.equipment.InvoiceRule;
import com.hoo.entity.equipment.LinkAdvertiseMachine;
import com.hoo.exception.SaleException;
import com.hoo.form.equipment.InvoiceMachineForm;
import com.hoo.mapper.equipment.AdvertiseMapper;
import com.hoo.mapper.equipment.InvoiceMachineMapper;
import com.hoo.util.PaginationSupport;

@Service
public class InvoiceMachineService {

	@Autowired
	InvoiceMachineMapper invoiceMachineMapper;
	
	@Autowired
	AdvertiseMapper advertiseMapper;
	
	public List<InvoiceMachine> getAllInvoiceMachine(){
		return invoiceMachineMapper.getAllInvoiceMachine();
	}
	
	@Transactional(readOnly = true)
	public InvoiceMachine getInvoiceMachine(InvoiceMachineForm form){
		return invoiceMachineMapper.getInvoiceMachine(form);
	}
	
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public int insertInvoiceMachine(InvoiceMachineForm form) throws SaleException{
		InvoiceMachine invoiceMachine = BeanMapper.map(form, InvoiceMachine.class);
		invoiceMachine.setAddTime(new Date());
		invoiceMachineMapper.insertInvoiceMachine(invoiceMachine);
		invoiceMachine = this.invoiceMachineMapper.getInvoiceMachine(form);
		if(invoiceMachine.getId()!=0){
			if (null != form.getAdveritseStr() && !"".equals(form.getAdveritseStr())){
				List<String> advertiseIdList = java.util.Arrays.asList(form
						.getAdveritseStr().split(","));
				LinkAdvertiseMachine linkAdvertiseMachine = null;
				for (String advertiseId : advertiseIdList) {
					linkAdvertiseMachine = new LinkAdvertiseMachine();
					linkAdvertiseMachine.setAdvertiseId(Integer
							.valueOf(advertiseId));
				    linkAdvertiseMachine.setInvoiceMachineId(invoiceMachine.getId());
					try {
						insertLinkAdvertiseMachine(linkAdvertiseMachine);
					} catch (Exception exception) {
						throw new SaleException();
					}
				}
			}
		}
		return 1;
	}
	
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public int updateInvoiceMachine(InvoiceMachineForm form)  throws SaleException{
		//将该取票机所使用的广告全部删除
		/*if(1 != this.advertiseMapper.deleteLinkAdvertiseMachineByMachineId(form.getId())){
			throw new SaleException("");
		}*/
		this.advertiseMapper.deleteLinkAdvertiseMachineByMachineId(form.getId());
		if(null != form.getAdveritseStr()){
			List<String> advertiseIdList = java.util.Arrays.asList(form
					.getAdveritseStr().split(","));
			LinkAdvertiseMachine linkAdvertiseMachine = null;
			for (String advertiseId : advertiseIdList) {
				linkAdvertiseMachine = new LinkAdvertiseMachine();
				linkAdvertiseMachine.setAdvertiseId(Integer
						.valueOf(advertiseId));
				linkAdvertiseMachine.setInvoiceMachineId(form.getId());
				if (1 != insertLinkAdvertiseMachine(linkAdvertiseMachine)) {
					throw new SaleException("");
				}
			}
		}
		InvoiceMachine invoiceMachine = BeanMapper.map(form, InvoiceMachine.class);
		invoiceMachine.setUpdateTime(new Date());
		if(1!=invoiceMachineMapper.updateInvoiceMachine(invoiceMachine)){
			throw new SaleException("");
		}
		return 1;
	}
	
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public int updateInvoiceMachineByIp(InvoiceMachine invoiceMachine){
		invoiceMachine.setUpdateTime(new Date());
		return invoiceMachineMapper.updateInvoiceMachine(invoiceMachine);
	}
	
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public int updateInvoiceMachineConnectStatus(int status){
		return invoiceMachineMapper.updateInvoiceMachineConnectStatus(status);
	}
	
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public int deleteInvoiceMachine(int ip){
		return invoiceMachineMapper.deleteInvoiceMachineById(ip);
	}
	
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public int insertLinkAdvertiseMachine(LinkAdvertiseMachine linkAdvertiseMachine){
		return this.advertiseMapper.insertLinkAdvertiseMachine(linkAdvertiseMachine);
	}
	
	/**
	 * 监控取票机状态
	 * @param invoiceMachines
	 * @return
	 */
	public List<InvoiceMachineForm> monitorInvoiceMachine(List<InvoiceMachine> invoiceMachines){
		List<InvoiceMachineForm> invoiceMachineForms=new ArrayList<InvoiceMachineForm>();
		for(int i=0;i<invoiceMachines.size();i++){
				InvoiceMachineForm invoiceMachineForm=new InvoiceMachineForm();
				invoiceMachineForm.setAddTime(invoiceMachines.get(i).getAddTime());
				invoiceMachineForm.setIp(invoiceMachines.get(i).getIp());
				invoiceMachineForm.setMacIp(invoiceMachines.get(i).getMacIp());
				invoiceMachineForm.setPosition(invoiceMachines.get(i).getPosition());
				invoiceMachineForm.setConnectStatus(invoiceMachines.get(i).getConnectStatus());//1为可以连接，2为未能连接
				invoiceMachineForms.add(invoiceMachineForm);
		}
		return invoiceMachineForms;
	}
	
	@Transactional(readOnly = true)
	public PaginationSupport<InvoiceMachine> searchInvoiceMachine(InvoiceMachineForm form) {
		int totalCount = this.invoiceMachineMapper.getInvoiceMachineCount(form);
		List<InvoiceMachine> list = this.invoiceMachineMapper.getInvoiceMachineList(form);
		return new PaginationSupport(list, totalCount);
	}
	
	@Transactional(readOnly = true)
	public int getInvoiceMachineCount(InvoiceMachineForm form){
		return this.invoiceMachineMapper.getInvoiceMachineCount(form);
	}
	
	/**
	 * 控制取票机的开关唤醒
	 * @param file
	 * @return
	 */
	public int controllComputer(String file) {
		int exitValue = -1;
		String[] cmd = new String[] { "cmd.exe", "/C", file }; // 命令
		try {
			Process process = Runtime.getRuntime().exec(cmd);
			exitValue = process.waitFor();
			process.getOutputStream().close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			return exitValue;
		}
	}

	@Transactional(readOnly = true)
	public InvoiceRule getInvoiceRule() {
		return this.invoiceMachineMapper.getInvoiceRule();
	}

	@Transactional(readOnly = true)
	public InvoiceMachine getInvoiceMachineById(int id) {
		return this.invoiceMachineMapper.getInvoiceMachineById(id);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public int updateInvoiceRule(InvoiceRule rule) {
		return this.invoiceMachineMapper.updateInvoiceRule(rule);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public int updateInvoiceMachineConnectStatusByIP(Map<String,Object> map) {
		return this.invoiceMachineMapper.updateInvoiceMachineConnectStatusByIP(map);
	}
}
